#ifndef WX_PRECOMP
#include "wx/wx.h"
#endif
#include "estdialog1.h"
#include <wx/string.h>
#include <vector>
#include "wx/filedlg.h"
using std::vector;
///////////////////////////////////////////////////////////////////////////

EstimationDialog::EstimationDialog( wxWindow* parent, wxWindowID id, const wxString& title, const wxPoint& pos, const wxSize& size, long style ) : wxDialog( parent, id, title, pos, size, style )
{
    this->SetSizeHints( wxDefaultSize, wxDefaultSize );

    wxBoxSizer* bSizer1;
    bSizer1 = new wxBoxSizer( wxVERTICAL );

    wxBoxSizer* bSizer2;
    bSizer2 = new wxBoxSizer( wxHORIZONTAL );

    wxStaticBoxSizer* sbSizer7;
    sbSizer7 = new wxStaticBoxSizer( new wxStaticBox( this, wxID_ANY, wxT("Chose Model") ), wxVERTICAL );

    m_radioBtn1 = new wxRadioButton( this, wxID_ANY, wxT("Fundamental Matrix"), wxDefaultPosition, wxDefaultSize, wxRB_GROUP );
    sbSizer7->Add( m_radioBtn1, 1,wxALIGN_CENTER|wxALL|wxEXPAND, 5 );

    m_radioBtn2 = new wxRadioButton( this, wxID_ANY, wxT("Homography"), wxDefaultPosition, wxDefaultSize, 0 );
    sbSizer7->Add( m_radioBtn2, 1, wxALIGN_CENTER|wxALL|wxEXPAND, 5 );

    bSizer2->Add( sbSizer7, 1, wxEXPAND| wxTOP, 20 );

    wxStaticBoxSizer* sbSizer2;
    sbSizer2 = new wxStaticBoxSizer( new wxStaticBox( this, wxID_ANY, wxT("Match File") ), wxVERTICAL );

    m_button1 = new wxButton( this, wxID_ANY, wxT("Open Match File"), wxDefaultPosition, wxDefaultSize, 0 );
    sbSizer2->Add( m_button1, 0, wxALIGN_CENTER|wxALL|wxEXPAND, 5 );

    m_button2 = new wxButton( this, wxID_ANY, wxT("Save Matches"), wxDefaultPosition, wxDefaultSize, 0 );
    sbSizer2->Add( m_button2, 0, wxALIGN_CENTER|wxALL|wxEXPAND, 5 );

    bSizer2->Add( sbSizer2, 1, wxEXPAND | wxTOP, 20 );

    wxStaticBoxSizer* sbSizer3;
    sbSizer3 = new wxStaticBoxSizer( new wxStaticBox( this, wxID_ANY, wxT("Manual Match Control") ), wxVERTICAL );

    m_button3 = new wxButton( this, wxID_ANY, wxT("Add Match"), wxDefaultPosition, wxDefaultSize, 0 );
    sbSizer3->Add( m_button3, 0, wxALIGN_CENTER|wxALL|wxEXPAND, 5 );

    m_button4 = new wxButton( this, wxID_ANY, wxT("Delete Match"), wxDefaultPosition, wxDefaultSize, 0 );
    sbSizer3->Add( m_button4, 0, wxALIGN_CENTER|wxALL|wxEXPAND, 5 );

    bSizer2->Add( sbSizer3, 1, wxEXPAND |  wxTOP, 20 );

    wxStaticBoxSizer* sbSizer6;
    sbSizer6 = new wxStaticBoxSizer( new wxStaticBox( this, wxID_ANY, wxT("Tools") ), wxVERTICAL );

    m_button7 = new wxButton( this, wxID_ANY, wxT("Load Image Pair"), wxDefaultPosition, wxDefaultSize, 0 );
    sbSizer6->Add( m_button7, 0, wxALIGN_CENTER|wxALL|wxEXPAND, 5 );

    m_button8 = new wxButton( this, wxID_ANY, wxT("Show Motion Model"), wxDefaultPosition, wxDefaultSize, 0 );
    sbSizer6->Add( m_button8, 0, wxALIGN_CENTER|wxALL|wxEXPAND, 5 );

    bSizer2->Add( sbSizer6, 1, wxEXPAND| wxTOP, 20 );

    bSizer1->Add( bSizer2, 0, wxEXPAND, 5 );

    wxStaticBoxSizer* sbSizer4;
    sbSizer4 = new wxStaticBoxSizer( new wxStaticBox( this, wxID_ANY, wxEmptyString ), wxHORIZONTAL );

    m_staticText1 = new wxStaticText( this, wxID_ANY, wxT("LOADED CORRESPONDENCES"), wxDefaultPosition, wxDefaultSize, wxALIGN_CENTRE );
    m_staticText1->Wrap( -1 );
    m_staticText1->SetFont( wxFont( wxNORMAL_FONT->GetPointSize(), 73, 94, 92, true, wxEmptyString ) );

    sbSizer4->Add( m_staticText1, 1, wxALL, 10 );

    bSizer1->Add( sbSizer4, 0, wxEXPAND, 10 );

    wxStaticBoxSizer* ssizer6;
    ssizer6 = new wxStaticBoxSizer( new wxStaticBox( this, wxID_ANY, wxEmptyString ), wxVERTICAL );

    m_correspondencescrolledWindow = new wxScrolledWindow( this, wxID_ANY, wxDefaultPosition, wxDefaultSize, wxVSCROLL );
    m_correspondencescrolledWindow->SetScrollRate( 5, 5 );

    m_correspondencesizer = new wxBoxSizer( wxVERTICAL );

    m_correspondencescrolledWindow->SetSizer( m_correspondencesizer );
    m_correspondencescrolledWindow->Layout();
    m_correspondencesizer->Fit( m_correspondencescrolledWindow );
    ssizer6->Add( m_correspondencescrolledWindow, 1, wxEXPAND | wxALL, 5 );

    bSizer1->Add( ssizer6, 1, wxEXPAND, 5 );

    wxStaticBoxSizer* sbSizer5;
    sbSizer5 = new wxStaticBoxSizer( new wxStaticBox( this, wxID_ANY, wxEmptyString ), wxHORIZONTAL );

    m_button10 = new wxButton( this, wxID_OK, wxT("Process"), wxDefaultPosition, wxDefaultSize, 0 );
    sbSizer5->Add( m_button10, 1, wxALL|wxRIGHT, 5 );

    m_button12 = new wxButton( this, wxID_CANCEL, wxT("Cancel"), wxDefaultPosition, wxDefaultSize, 0 );
    sbSizer5->Add( m_button12, 1, wxALL|wxLEFT, 5 );

    bSizer1->Add( sbSizer5, 0, wxEXPAND, 5 );

    this->SetSizer( bSizer1 );
    this->Layout();

    // Connect Events
    m_button1->Connect( wxEVT_COMMAND_BUTTON_CLICKED, wxCommandEventHandler( EstimationDialog::OnOpenMatch ), NULL, this );
    m_button2->Connect( wxEVT_COMMAND_BUTTON_CLICKED, wxCommandEventHandler( EstimationDialog::OnSaveMatch ), NULL, this );
    m_button3->Connect( wxEVT_COMMAND_BUTTON_CLICKED, wxCommandEventHandler( EstimationDialog::OnAddMatch ), NULL, this );
    m_button4->Connect( wxEVT_COMMAND_BUTTON_CLICKED, wxCommandEventHandler( EstimationDialog::OnDeleteMatch ), NULL, this );
    m_button7->Connect( wxEVT_COMMAND_BUTTON_CLICKED, wxCommandEventHandler( EstimationDialog::OnLoadImages ), NULL, this );
    m_button8->Connect( wxEVT_COMMAND_BUTTON_CLICKED, wxCommandEventHandler( EstimationDialog::OnShowMotionModel ), NULL, this );
    m_button10->Connect( wxEVT_COMMAND_BUTTON_CLICKED, wxCommandEventHandler( EstimationDialog::OnProcess ), NULL, this );
}

EstimationDialog::~EstimationDialog()
{
    // Disconnect Events
    m_button1->Disconnect( wxEVT_COMMAND_BUTTON_CLICKED, wxCommandEventHandler( EstimationDialog::OnOpenMatch ), NULL, this );
    m_button2->Disconnect( wxEVT_COMMAND_BUTTON_CLICKED, wxCommandEventHandler( EstimationDialog::OnSaveMatch ), NULL, this );
    m_button3->Disconnect( wxEVT_COMMAND_BUTTON_CLICKED, wxCommandEventHandler( EstimationDialog::OnAddMatch ), NULL, this );
    m_button4->Disconnect( wxEVT_COMMAND_BUTTON_CLICKED, wxCommandEventHandler( EstimationDialog::OnDeleteMatch ), NULL, this );
    m_button7->Disconnect( wxEVT_COMMAND_BUTTON_CLICKED, wxCommandEventHandler( EstimationDialog::OnLoadImages ), NULL, this );
    m_button8->Disconnect( wxEVT_COMMAND_BUTTON_CLICKED, wxCommandEventHandler( EstimationDialog::OnShowMotionModel ), NULL, this );
    m_button10->Disconnect( wxEVT_COMMAND_BUTTON_CLICKED, wxCommandEventHandler( EstimationDialog::OnProcess ), NULL, this );
}



void EstimationDialog::OnOpenMatch(wxCommandEvent& event)
{

    wxString caption = wxT("Choose a file");
    wxString wildcard =
        wxT("TXT files (*.txt)|*.txt");
    wxString defaultDir = wxT(".");
    wxString defaultFilename = wxEmptyString;
    wxFileDialog dialog(this, caption, defaultDir, defaultFilename,wildcard, wxOPEN);
    if (dialog.ShowModal() == wxID_OK)
    {
        wxString path = dialog.GetPath();
        printf("path is %s \n",path.ToAscii());
        int filterIndex = dialog.GetFilterIndex();
    }
}

void EstimationDialog::OnSaveMatch(wxCommandEvent& event)
{



}
void EstimationDialog::OnAddMatch(wxCommandEvent& event)
{

    guiCorrespondence* newcorr=new guiCorrespondence(m_correspondencescrolledWindow);
    corrGuiVector.push_back(newcorr);
    m_correspondencesizer->Add( newcorr, 0,  wxEXPAND|wxALIGN_TOP, 1 );
    Layout();
    GetSizer()->Fit( this );
}
void EstimationDialog::OnDeleteMatch(wxCommandEvent& event)
{

    if (corrGuiVector.size()==0) return;
    guiCorrespondence* lastAdded=corrGuiVector.back();






    lastAdded->deleteCTRLS();


    m_correspondencesizer->Remove(corrGuiVector.size()-1);

    corrGuiVector.pop_back();
    Layout();
    GetSizer()->Fit( this );
}
void EstimationDialog::OnLoadImages(wxCommandEvent& event)
{



}
void EstimationDialog::OnShowMotionModel(wxCommandEvent& event)
{



}
void EstimationDialog::OnProcess(wxCommandEvent& event)
{
    int nArraySize = (int)corrGuiVector.size();

    // display the total amount of elements on the screen
    printf("Total Elements: %d\n\n", nArraySize);

    // display the array's contents on the screen
    for (int i = 0; i < nArraySize; i++)
    {
        corrGuiVector[i]->TransferDataFromWindow();
        printf("match %d:  x1=%f \t y1=%f  \t\t x2=%f \t y2=%f \n", i, corrGuiVector[i]->GetX1(),corrGuiVector[i]->GetY1(),corrGuiVector[i]->GetX2(),corrGuiVector[i]->GetY2());
    }

}




guiCorrespondence::~guiCorrespondence()
{


}

guiCorrespondence::guiCorrespondence( wxWindow* parent, wxWindowID id, const wxPoint& pos , const wxSize& size)
        :wxStaticBoxSizer(wxHORIZONTAL,parent)
{
    m_corr_x1 = new wxTextCtrl(parent, wxID_ANY,wxT("0.0"), wxDefaultPosition, wxSize( 80, wxDefaultCoord ) );
    this->Add( m_corr_x1, 1, wxALL|wxEXPAND, 10 );

    m_corr_y1 = new wxTextCtrl( parent, wxID_ANY, wxT("0.0"), wxDefaultPosition, wxSize( 80, wxDefaultCoord ) );
    this->Add( m_corr_y1, 1, wxALL|wxEXPAND, 10 );

//spacer
    this->Add( 0, 0, 1, wxEXPAND, 5 );

    m_corr_x2 = new wxTextCtrl( parent, wxID_ANY, wxT("0.0"), wxDefaultPosition, wxSize( 80, wxDefaultCoord ) );
    this->Add( m_corr_x2, 1, wxALL|wxEXPAND, 10 );

    m_corr_y2= new wxTextCtrl( parent, wxID_ANY, wxT("0.0"), wxDefaultPosition, wxSize( 80, wxDefaultCoord ) );
    this->Add(m_corr_y2, 1, wxALL|wxEXPAND, 10 );





}

void guiCorrespondence::deleteCTRLS()
{
    m_corr_x1->Destroy();
    m_corr_y1->Destroy();
    m_corr_x2->Destroy();
    m_corr_y2->Destroy();

}

bool guiCorrespondence::TransferDataToWindow()
{

    wxString sx1;
    wxString sy1;
    wxString sx2;
    wxString sy2;



    sx1.Printf(wxT("%d"), x1);
    sx2.Printf(wxT("%d"), x2);
    sy1.Printf(wxT("%d"), y1);
    sy2.Printf(wxT("%d"), y2);




    m_corr_x1->SetValue(sx1);
    m_corr_x2->SetValue(sx2);
    m_corr_y1->SetValue(sy1);
    m_corr_y2->SetValue(sy2);
    printf("TransferDataToWindow\n");
    return true;
}

bool guiCorrespondence::TransferDataFromWindow()
{
    wxString sx1;
    wxString sy1;
    wxString sx2;
    wxString sy2;

    sx1= m_corr_x1->GetValue();
    sx2= m_corr_x2->GetValue();
    sy1= m_corr_y1->GetValue();
    sy2= m_corr_y2->GetValue();

    sx1.ToDouble(&x1);
    sx2.ToDouble(&x2);
    sy1.ToDouble(&y1);
    sy2.ToDouble(&y2);
    printf("TransferDataFromWindow\n");
    return true;
}
